<?php
namespace app\yxwadmin\controller;
use app\yxwadmin\controller\Admin;
use think\Controller;
use think\Request;
use think\Session;

/**
* 管理员管理
*/
class Manager extends Admin{


	public function index(){
        $this->assign('uid', session('uid'));
		return $this->view->fetch("manager/index");
    }

    public function getList(){
        $limit = input('limit','0');
        $offset = input('offset','0');
        $sortName = input('sortName','id');
        $sortOrder = input('sortOrder','asc');
        $keyword = input('keyword','');
        $where['user_name|real_name'] = array('like','%'.$keyword.'%');
        $UserModel = model('UserModel');
        $count = $UserModel->where($where)->count(); //查询满足要求的总记录数
        $list = $UserModel
        ->alias('a')
        ->field('a.id,a.user_name,a.real_name,a.status,a.last_login_ip,a.last_login_time,b.roleid,c.role_name')
        ->join('Roleinusers b','b.userid=a.id','LEFT')
        ->join('Role c','c.id=b.roleid','LEFT')
        ->group('a.id')
        ->where($where)
        ->order(array($sortName=>$sortOrder))
        ->limit($offset.','.$limit)
        ->select();
        $data['total'] = $count;
        $data['rows'] = $list;
        return $data;
    }

    public function add(){
        //判断提交方式 做不同的处理
        if (Request::instance()->isPost()){
            $now = time();
            //实例化Votes对象
            $data=input('post.');                        
            $data['update_time'] = $now;
            $data['create_time'] = $now;
            $data['pass_word'] = md5($data['pass_word']);
            //更新数据库
            if($id=model('UserModel')->insert($data)){
                $ret["code"]=0;
                $ret['msg']="新增成功";
            }
            else{
                $ret["code"]=1;
                $ret['msg']="新增失败";
            }
            return $ret;
        }else{            
            return $this->view->fetch('manager/add');
        }
    }

    public function edit(){
        $UserModel = model('UserModel');
        //判断提交方式 做不同的处理
        if (Request::instance()->isPost()){
            $res;
            $data=input('post.');            
            $data['update_time'] = time();
            if($data['pass_word']){
                $data['pass_word'] = md5($data['pass_word']);
                $res = $UserModel->allowField(['user_name','pass_word','real_name','status','update_time'])->save($data,['id' => $data['id']]);
            }else{                
                $res = $UserModel->allowField(['user_name','real_name','status','update_time'])->save($data,['id' => $data['id']]);
            }
            if($res){
                $ret["code"]=0;
                $ret['msg']="修改成功";
            }else{
                $ret["code"]=1;
                $ret['msg']="修改失败";                
            }
            return $ret;
        }else{
            $id = input('get.id','0');
            $data = $UserModel->find($id);
            $this->assign('data',$data);
            return $this->view->fetch('manager/edit');
        }
    }

    public function enable(){
        if(Request::instance()->isPost() ){            
            $id = input('id');
            $status = input('status');
            $res = model('UserModel')->where('id',$id)->setField(['status'=>$status]);
            if ($res) {
                $ret["code"]=0;
                $ret['msg']="更新成功";
                return $ret;
            }else {
                $ret["code"]=1;
                $ret['msg']="更新失败";
                return $ret;
            }
        }else{
            $ret["code"]=1;
            $ret['msg']="非法操作";
            return $ret;
        }
    }

    public function getuserlist(){
        $limit = input('post.limit','0','trim');
        $offset = input('post.offset','0','trim');
        $sortName = input('post.sortName','id','trim');
        $sortOrder = input('post.sortOrder','asc','trim');
        $keyword = input('post.keyword','','trim');
        $roleid = input('post.roleid','','trim');
        $roleusers = model("RoleInUsersModel")->field("userid")->where("roleid",$roleid)->column("userid");
            
        $where['real_name']=array('like','%'.$keyword.'%');        

        $UserModel = model('UserModel');
        $count = $UserModel->where($where)->where('id','not in',$roleusers)->where('id','neq',1)->count(); //查询满足要求的总记录数
        $list = $UserModel->where($where)->where('id','not in',$roleusers)->where('id','neq',1)->field('id,real_name')->order($sortName.' '.$sortOrder)->limit($offset,$limit)->select();                

        $data['total'] = $count;
        $data['rows'] = $list;
        return $data;
    }

    public function center(){
        $where['id'] = Session('uid');      
        $user = model("UserModel")->where($where)->field('id,user_name,real_name')->find();
        $this->assign('user', $user);

		return $this->view->fetch('manager/center');
    }

    public function centersave(){
        $data['update_time'] = time();
        $uid = input('post.uid/d');
        $username = input('post.user_name');
        $realname = input('post.real_name');
    	$password_new = input('post.password_new');
        $password_re = input('post.password_re');
    	$where['id'] = Session::get('uid');
        $UserModel = model('UserModel');
    	$user = $UserModel->where($where)->field('user_name')->find();
        $returnArr['code'] = 1; //0 表示修改成功的返回代码，1 表示修改失败
    	if ($uid === $where['id']) {
            if ($realname) {
                $data['real_name'] = $realname;
            }
    		if ($password_new && $password_re) {
    			if ($password_new === $password_re) {
                    $data['pass_word'] = md5($password_new);
    			}else {
                    echo json_encode($returnArr);
                    exit();
                }
	    	}
            $res = $UserModel->where($where)->update($data);
            if ($res) {
                $returnArr['code'] = 0;
            }
            if ($password_new && $password_re) {
                session(null);
                session_destroy();
            }
            
    	}

        echo json_encode($returnArr);
    }    
}